package com.booking.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.management.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.booking.bean.Foodorder;
import com.booking.bean.Users;
import com.booking.dao.OrderDAO;

@Repository
public class OrderDAOImpl implements OrderDAO {
	
	//依赖注入SessionFactory
//	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//	 SessionFactory sessionFactory = (SessionFactory) ctx.getBean(LocalSessionFactoryBean.class);
//	
	@Autowired
	private SessionFactory sessionFactory;
	//获取和当前线程绑定的 Session. 
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	


	@Transactional
	@Override
	public int createOrder(Foodorder order) {
		int id = 0;
		String result = null;
		result =  String.valueOf( this.getSession().save(order)) ;
		
		//this.getSession().update(sql,2);
		
		if(result != null){
			String hql="from Foodorder u where u.userid = ? order by orderdate desc";
			org.hibernate.Query query = this.getSession().createQuery(hql); 
			
			query.setLong(0,order.getUserid());
			List list = query.list();
				if(list.size()>0){
					Foodorder  f =  (Foodorder) list.get(0);
					id = f.getId();
					return id;
				}
		}
		
		return id;
	}

	@Transactional
	@Override
	public  List<Foodorder> queryOrder(int status,int id) {
		String hql="select f  from Foodorder f where f.status = ? and f.userid = ? and  rownum <=10 order by orderdate desc  ";
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		query.setLong(0,status);
		query.setLong(1,id);
		List<Foodorder> list = query.list();
		
		
		return list;
	}
	@Transactional
	@Override
	public  List<Foodorder> queryNoneOrder(int status,int id) {
		String hql="select f  from Foodorder f where f.status = ? and f.sellerid = ? order by orderdate desc";
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		query.setLong(0,status);
		query.setLong(1,id);
		List<Foodorder> list = query.list();
		
		
		return list;
	}

	@Transactional
	@Override
	public void deleteOrder(int id) {
		
		//删除方法一
		Foodorder order = new Foodorder();
		order.setId(id);
		this.getSession().delete(order);
		
		//删除方法二
		//	String hql = "delete from Foodorder c where c.id = (" + id + ")";
//		this.getSession().createQuery(hql).executeUpdate();
	}

	@Transactional
	@Override
	public void doneByIds(String ids) {
		String hql = "update Foodorder f set f.status = '1' where f.id in (" + ids + ")";
		this.getSession().createQuery(hql).executeUpdate();
		
	}
}
